﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Business
{
    public partial class frm_Edit_ImportOrderClearance : Form
    {
        private INISettings mySettings;
        private SqlConnection myConnection;

        public string id;

        public frm_Edit_ImportOrderClearance()
        {
            InitializeComponent();
            this.mySettings = new INISettings();
            this.myConnection = new SqlConnection(mySettings.SQLConnectionString);
            this.id = null;
            LoadClearanceList();
        }

        /////////////////////////////////////
        /// Load Clearance List
        /////////////////////////////////////
        private void LoadClearanceList()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT ID FROM [241_CustomClearance] ORDER BY ID";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["ID"].ToString();
                    item.Value = dataReader["ID"].ToString();
                    this.clearanceNo.Properties.Items.Add(item);
                }
                dataReader.Close();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }                
            }
        }


        //////////////////////////////////////////
        /// Load Data
        ///////////////////////////////////////////        
        public bool LoadData()
        {
            bool ret = true;
            if (string.IsNullOrEmpty(this.id))
            {
                ret = false;
            }
            else
            {
                try
                {
                    myConnection.Open();
                    SqlCommand sql_cmd = myConnection.CreateCommand();
                    sql_cmd.CommandText = "SELECT * FROM [241_ImportOrderClearance] WHERE ID=" + this.id;
                    SqlDataReader dataReader = sql_cmd.ExecuteReader();
                    if (dataReader.Read())
                    {
                        for (int i = 0; i < this.clearanceNo.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.clearanceNo.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["ClearanceNo"].ToString().Trim())
                            {
                                this.clearanceNo.SelectedIndex = i;
                            }
                        }
                        this.orderNo.Text = dataReader["OrderNo"].ToString().Trim();
                    }
                    dataReader.Close();
                    sql_cmd.Dispose();
                    myConnection.Close();
                }
                catch
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    ret = false;
                }
            }
            return ret;
        }


        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {            
            bool duplicate = false;
            if (this.clearanceNo.SelectedIndex < 0)
            {
                MessageBox.Show("Please select Clearance No.");
            }
            else
            {
                ComboboxItem item = (ComboboxItem)this.clearanceNo.SelectedItem;
                //Check Duplicate
                try
                {
                    myConnection.Open();
                    SqlCommand cmd_sql = myConnection.CreateCommand();
                    cmd_sql.CommandText = "SELECT COUNT(*) FROM [241_ImportOrderClearance]"
                        + " WHERE ClearanceNo='" + item.Value.ToString() + "' AND OrderNo='" + this.orderNo.Text + "'";
                    SqlDataReader dataReader = cmd_sql.ExecuteReader();
                    if (dataReader.Read())
                    {
                        if (dataReader[0] != DBNull.Value)
                        {
                            if (Int32.Parse(dataReader[0].ToString()) > 0)
                            {
                                duplicate = true;
                            }
                        }
                    }
                    dataReader.Close();
                    myConnection.Close();
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    duplicate = true;
                }

                if (duplicate)
                {
                    MessageBox.Show("The Clearance No.: " + item.Value.ToString() + " AND Order No.: " + this.orderNo.Text + " existed");
                }
                else
                {
                    try
                    {
                        myConnection.Open();
                        string sql = "UPDATE [241_ImportOrderClearance] "
                                + "SET ClearanceNo='" + item.Value.ToString() + "', OrderNo='" + this.orderNo.Text + "' "
                                + "WHERE ID=" + this.id;                                
                        SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                        cmd_sql.ExecuteNonQuery();
                        cmd_sql.Dispose();
                        myConnection.Close();
                        MessageBox.Show("Order-Clearance has been updated Success!");
                        this.Dispose();
                        //clearForm();
                    }
                    catch //(Exception ex)
                    {
                        if (myConnection.State == ConnectionState.Open)
                        {
                            myConnection.Close();
                        }
                        //MessageBox.Show(ex.Message);
                        MessageBox.Show("Cannot update Order-Clearance. Please try again!");
                    }
                }
            }
        }        
    }
}
